<%@ page language="java" contentType="text/html; charset=UTF-8"
	import="java.util.*,com.cap.bts.crmdp.dealer.meal.dto.ResultDto"
	pageEncoding="UTF-8"%>
<%
    ArrayList resultList = (ArrayList) request
					.getAttribute("resultList");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>入会礼管理</title>
<%@include file="../include/include.jsp"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css" />
<script type="text/javascript" src="${jspath}/jquery/jquery-2.1.3.js"></script>
<link rel="stylesheet" type="text/css"
	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script>
<script type="text/javascript"
	src="${bootstrapPath}/js/bootstrap3-validation.js"></script>

<script type="text/javascript" src="${layerPath}/layer.js"></script>
</head>
<style>
.main {
	position: relative;
	margin: 0 auto;
	padding: 10px 10px;
	width: 570px;
	height: auto;
}

.btnWidth {
	width: 100px;
}

.hr {
	text-align: center;
	width: 100%;
	background-color: #0071C1;
	height: 1px;
	margin-bottom: 10px;
}
</style>

<script type="text/javascript">
	var giftCount = 0;
	$(function() {
		$("#returnBtn")
				.click(
						function() {
							var packageId = $("#packageId").val();
							var qName = $("#qName").val();
							var qCode = $("#qCode").val();
							var qStatus = $("#qStatus").val();
							var pager = $("#pager").val();
							location.href = '${webcontext}/crmdp/dealer/meal/toEditMealPage?id='
									+ packageId+"&qName="+qName+"&qCode="+qCode+"&qStatus="+qStatus+"&pager.currentPage="+pager;
						});
		
		$("#saveBtn").click(function(){
			//入会礼
			var giftStr = "";
			if(giftCount >= 1){
				for ( var k = 0; k < giftCount; k++) {
					var giftName = $("#giftName"+k).val();
					var giftType = $("#giftType"+k).val();
					var giftNameText = $("#giftName"+k+" option:selected").text();
					if (giftName != "N/A" && giftName != undefined
							&& giftName != null
							&& giftName != "") {
						giftStr = giftStr + giftName + "@#" + giftType + "@#" + giftNameText + "###";
					}
				}
				if(giftStr == ""){
					jAlert("没有新增任何数据","无法提交");
					return false;
				}
			}
			
			$
			.ajax({
				url : '${webcontext}/crmdp/dealer/meal/saveMermberGiftInfo',
				type : 'post',
				data : {
					'packageId':$("#packageId").val(),
					'giftStr':giftStr
				},
				success : function(data) {
					if (data.code == true) {
						jAlert(
								data.msg,
								"提示",
								function() {
									var packageId = $("#packageId").val();
									var qName = $("#qName").val();
									var qCode = $("#qCode").val();
									var qStatus = $("#qStatus").val();
									var pager = $("#pager").val();
									var temp = $("#temp").val();
									location.href = '${webcontext}/crmdp/dealer/meal/toMemberGiftPage?id='
											+ packageId+"&qName="+qName+"&qCode="+qCode+"&qStatus="+qStatus+"&pager.currentPage="+pager+"&temp="+temp;
								});
					} else if (data.code == false) {
						jAlert(data.msg,"提示");
					}
				},
				error : function() {
					jAlert("服务器错误，请联系管理员","提示");
				}
			});
		});
	});

	//入会礼新增
	function insertMermberGift() {
		var table = $("#data_tabs");
		var row = $("<tr></tr>");
		var td0 = $("<td class='hidden'></td>");
		var td1 = $("<td class='hidden'></td>");
		var td2 = $("<td></td>");
		var td3 = $("<td></td>");
		var td4 = $("<td></td>");
		row.append(td0);
		row.append(td1);
		td2
				.append($("<select id='giftName"
						+ giftCount
						+ "'  name='giftName' class='form-control input-sm' onchange='changeType(this[selectedIndex].value,this.id)'><option value='N/A'>--------请选择项目---------</option><core:forEach items='${resultList}' var='list'><option value='${list.id}'>${list.name}</option></core:forEach></select>"));
		row.append(td2);
		td3
				.append($("<input class='form-control input-sm' id='giftTypeText"
				+ giftCount
				+ "' type='text' readonly /> <input class='form-control input-sm hidden' id='giftType"
				+ giftCount
				+ "' type='text' readonly />"));
		row.append(td3);
		td4
				.append($("<button type='button' id='delete"
						+ giftCount
						+ "' class='btn btn-warning btn-xs' onclick='deleteRow(this.id)'>删除</button>"));
		row.append(td4);
		table.append(row);
		giftCount++;

	}
	
	//自加行选中项目列表中的值，改变类型的值
	function changeType(value, id) {
		var num = id.substring(id.length - 1, id.length);
		if (!isNaN(num)) {
	<%if (resultList != null) {%>

	<%for (int k = 0; k < resultList.size(); k++) {
					ResultDto resultDto = (ResultDto) resultList.get(k);%>
	if (value =='<%=resultDto.getId()%>'
	) {
		var typeText = '<%=resultDto.getTypeText()%>';
		var type = '<%=resultDto.getType()%>';
				$("#giftTypeText" + num).val(typeText);
				$("#giftType" + num).val(type);
			}
<%}%>
	
<%}%>
	}
	}

	//删除
	function removeMealLine(id) {
		//询问框
		jConfirm(
				"确定要删除数据吗？",
				"确认",
				function(r) {
					if (r) {
						$
								.ajax({
									url : '${webcontext}/crmdp/dealer/meal/removeMermberGift',
									type : 'post',
									data : {
										'id' : id
									},
									success : function(data) {
										if (data.code == true) {
											jAlert(
													data.msg,
													"提示",
													function() {
														var packageId = $(
																"#packageId")
																.val();
														var qName = $("#qName").val();
														var qCode = $("#qCode").val();
														var qStatus = $("#qStatus").val();
														var pager = $("#pager").val();
														var temp = $("#temp").val();
														location.href = '${webcontext}/crmdp/dealer/meal/toMemberGiftPage?id='
																+ packageId+"&qName="+qName+"&qCode="+qCode+"&qStatus="+qStatus+"&pager.currentPage="+pager+"&temp="+temp;
													});
										} else if (data.code == false) {
											jAlert(data.msg, "提示");
										}
									},
									error : function() {
										jAlert("服务器错误，请联系管理员", "提示");
									},
								});
					}
				});
	}

	//删除行
	function deleteRow(id) {
		$("#" + id).parent().parent().remove();
	}
</script>
<body>
	<div id="content" style="height: 950px;">
		<div style="color: #025D7B; height: 26px; line-height: 26px;">
			<span>套餐定义</span> <span>&gt;</span> <span>入会礼管理</span>
		</div>
		<div class="hr"></div>
		<form:form id="search_form" action="" class="form-horizontal"
			method="post" modelAttribute="dto" role="form">
			
			<input type="text" class="hidden" id="packageId" value="${id}" />
			<input type="text" class="hidden" name="qName" id="qName" value="${qName}">
			<input type="text" class="hidden" name="qCode" id="qCode" value="${qCode}">
			<input type="text" class="hidden" name="qStatus" id="qStatus"
				value="${qStatus}">
			<input type="text" class="hidden" id="pager" name="pager.currentPage"
				value="${currentPage}">
				
			<input type="text" class="hidden" name="temp" id="temp" value="${temp}">
			
			<table id="data_tabs" class="table"
				style="margin-top: 20px; margin-left: 20px; width: 98%;">
				<thead>
					<tr>
						<th width="5%" class="hidden">ID</th>
						<th width="5%" class="hidden">礼物ID</th>
						<th width="20%">项目</th>
						<th width="20%">类型</th>
						<th width="15%">操作</th>
					</tr>
				</thead>
				<tbody>
					<core:forEach items="${pGiftDtoList}" var="list">
						<tr>
							<td class="text-left hidden">${list.id}</td>
							<td class="text-left hidden">${list.giftId}</td>
							<td class="text-left">${list.giftName}</td>
							<td class="text-left"><core:choose>
									<core:when test="${list.giftType=='COUPON'}">
										优惠券
									</core:when>
									<core:when test="${list.giftType=='REDEMPTION'}">
										提货券
									</core:when>
									<core:when test="${list.giftType=='PACKAGE'}">
										套餐
									</core:when>
								</core:choose></td>
							<td class="text-left">
								<core:choose>
									<core:when test="${temp == true}">
										<button type="button" class="btn btn-success btn-xs btnLength"
											onClick="removeMealLine('${list.id}')" disabled>
											<i class="glyphicon glyphicon-trash" style="color: #FFFFFF"></i>&nbsp;删除
										</button>
									</core:when>
									<core:when test="${temp == false}">
										<button type="button" class="btn btn-success btn-xs btnLength"
											onClick="removeMealLine('${list.id}')">
											<i class="glyphicon glyphicon-trash" style="color: #FFFFFF"></i>&nbsp;删除
										</button>
									</core:when>
								</core:choose>
							</td>
						</tr>
					</core:forEach>
				</tbody>
			</table>
			<div class="text-center">
				<core:choose>
					<core:when test="${temp == true}">
						<button type="button" class="btn btn-success btn-sm"
							onclick="insertMermberGift()" disabled>
							<i class="glyphicon glyphicon-plus" style="color: #FFFFFF"></i>&nbsp;新增
						</button>
						<button type="button" class="btn btn-success btn-sm" id="saveBtn" disabled>
							<i class="glyphicon glyphicon-floppy-saved" style="color: #FFFFFF"></i>&nbsp;保存
						</button>
					</core:when>
					<core:when test="${temp == false}">
						<button type="button" class="btn btn-success btn-sm"
							onclick="insertMermberGift()">
							<i class="glyphicon glyphicon-plus" style="color: #FFFFFF"></i>&nbsp;新增
						</button>
						<button type="button" class="btn btn-success btn-sm" id="saveBtn">
							<i class="glyphicon glyphicon-floppy-saved" style="color: #FFFFFF"></i>&nbsp;保存
						</button>
					</core:when>
				</core:choose>
				<button type="button" class="btn btn-success btn-sm" id="returnBtn">
					<i class="glyphicon glyphicon-retweet" style="color: #FFFFFF"></i>&nbsp;返回
				</button>
			</div>
		</form:form>
	</div>
</body>
</html>